package co.edu.uniandes.entregAndes.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import co.edu.uniandes.entregAndes.fachada.EntregAndesAdmin;
import co.edu.uniandes.entregAndes.vos.EnviosValue;

/**
 * Servlet implementation class ServletAdmin
 */
public class ServletAdmin extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private static EntregAndesAdmin entregAndesAdmin = EntregAndesAdmin
			.darInstancia();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ServletAdmin() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String requerimiento = request.getParameter("requerimiento");

		if (requerimiento.equals("consultar")) {
			ArrayList<EnviosValue> resp = new ArrayList<EnviosValue>();
			String valorCriterio = "";
			if (request.getParameter("tipoCriterio").equals("ciudad_origen")
					|| request.getParameter("tipoCriterio").equals(
							"ciudad_destino")) {
				valorCriterio = request.getParameter("codPostalCiudad");
			} else {
				valorCriterio = request.getParameter("valorCriterio");
			}
			resp.addAll(entregAndesAdmin.respConsultaEnviosValueMetRF1(
					request.getParameter("tipoCriterio"), valorCriterio,
					request.getParameter("orden")));
			PrintWriter pw = response.getWriter();
			if (!resp.isEmpty()) {
				for (EnviosValue ev : resp) {
					pw.println("<b>" + ev.darId() + "</b><br>");
					pw.println("-- Estado:" + ev.darEstado());
					pw.println("<br>-- Destino:" + ev.darCiudadDestino()
							+ "<br>");
				}
			} else {
				pw.print("No hay");
			}
		} else if (requerimiento.equals("actualizar")) {
			try {
				entregAndesAdmin.consultaMetRF4(request
						.getParameter("datosEnvio"));
				response.getWriter().print("<html>");
				response.getWriter().print("<head></head>");
				response.getWriter().print("<body>");
				response.getWriter().println(
						"Realizado Satisfactoriamente.<br>");
				response.getWriter().println(
						"<a href=\"/EntregAndes/\">Volver</a>");
				response.getWriter().print("</body>");
				response.getWriter().print("</html>");
			} catch (Exception e) {
				response.getWriter()
						.println(
								"Serious error.\n All shortcuts have disappeared.\n Screen. Mind. Both are blank.");
			}
		} else if (requerimiento.equals("modificar")) {
			try {
				entregAndesAdmin.consultaMetRF5(
						Integer.parseInt(request.getParameter("idEnvio")),
						request.getParameter("nuevaDireccion"),
						request.getParameter("codPostalCiudad"),
						request.getParameter("emailDestino"),
						request.getParameter("tipoPersona"));
				response.getWriter().println("Realizado Satisfactoriamente.");
			} catch (Exception e) {
				response.getWriter().println(e.getMessage());
			}
		} else if (requerimiento.equals("registrar")) {
			String rta = entregAndesAdmin.consultaMetRF3(
					request.getParameter("peso"),
					request.getParameter("direccionOrigen"),
					request.getParameter("direccionDestino"),
					request.getParameter("nombre_tipo"),
					request.getParameter("volumen"),
					request.getParameter("correo_remitente"),
					request.getParameter("cod_postal_origen"),
					request.getParameter("correo_destinatario"),
					request.getParameter("cod_postal_destino"),
					request.getParameter("nombre_oficina_origen"),
					request.getParameter("tipo_persona"));
			response.getWriter().print("<html>");
			response.getWriter().print("<head></head>");
			response.getWriter().print("<body>");
			response.getWriter().println(
					"Realizado Satisfactoriamente. El costo es " + rta);
			response.getWriter().print(
					"<br><a href=\"/EntregAndes/operator/\">Aceptar</a><br>");
			response.getWriter()
					.print("<form action=\"/EntregAndes/operator/process\" method=\"get\">");
			response.getWriter()
					.print("<input type=\"hidden\" id=\"requerimiento\" name=\"requerimiento\" value=\"eliminarEnvioNuevo\">");
			response.getWriter().print(
					"<button type=\"submit\">No Aceptar</button>");
			response.getWriter().print("</form>");
			response.getWriter().print("<body>");
			response.getWriter().print("</html>");
		} else if (requerimiento.equals("eliminarEnvioNuevo")) {
			try {
				entregAndesAdmin.darDao().eliminarUltimoEnvio();
				response.getWriter().print("Realizado Satisfactoriamente.");
			} catch (Exception e) {
				response.getWriter()
						.println(
								"Serious error.\n All shortcuts have disappeared.\n Screen. Mind. Both are blank.");
			}
		} else {
			response.getWriter()
					.println(
							"Serious error.\n All shortcuts have disappeared.\n Screen. Mind. Both are blank.");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
}
